Q:
What is the maximum number of files that can be simultaneously open in
a Mac OS X application?
A:
By default, Mac OS X imposes a limit of 256 simultaneously open files per-process.
The actual number of files your application may open simultaneously will
be slightly smaller than this amount: the actual number of files your
application can open simultaneously will be reduced by the number of files
opened by the services used by your application. For instance, some files, such
as stdin , are opened for you by the system, and other
files may be opened by frameworks called by your application.
If an application needs to have more than 256 files open
simultaneously, then it can raise this limit using the
setrlimit System framework call as shown in Listing 1.
Note:
setrlimit is a System framework call. As such, CFM applications
will need to use the CFBundle APIs in order to call this routine.
|
setrlimit can be used to increase the maximum number
of files an application can open, but this value has some practical
limitations. By default, there is a global limit for the total number of
open files: this limit is set to the value 12288. Also, each unique open
file consumes a vnode in the Virtual-Filesystem Interface layer.
The number of available vnodes is a function of installed RAM (on a
128M system, there will be approximately 2864 vnodes available).
During normal system operation, the system will maintain between 300 and 400
open files.
#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/time.h>#include <sys/resource.h>#include <sys/errno.h>
int SetMaximumFiles(long filecount) {
struct rlimit lim;
lim.rlim_cur = lim.rlim_max = (rlim_t)filecount;
if ( setrlimit(RLIMIT_NOFILE, &lim) == 0 )
return 0;
else return errno;
}
int GetMaximumFiles(long *filecount) {
struct rlimit lim;
if ( getrlimit(RLIMIT_NOFILE, &lim) == 0 ) {
*filecount = (long)lim.rlim_max;
return 0;
} else return errno;
}
int main (int argc, const char * argv[])
{
long filecount;
int err;
printf("SetMaximumFiles(800)=%d\n", SetMaximumFiles(800));
err = GetMaximumFiles(&filecount);
printf("GetMaximumFiles(&filecount)=%d\n", err);
if (err == 0) printf(" -> filecount = %ld\n", filecount);
return 0;
}
/* output from this program:
SetMaximumFiles(800)=0
GetMaximumFiles(&filecount)=0
-> filecount = 800
it has exited with status 0.
*/
|
Listing 1. Example program that adjusts the open file limit.
|
Advanced applications can override global open file limit and the
global vnode quota at runtime using the sysctl routine.
However, this operation requires root permission and is intended only for configuring
servers and other types of professionally maintained machines.
IMPORTANT:
Software intended for use on general desktop systems should not attempt to
adjust the open file limit or the global vnode quota at runtime. Adjusting the
number of vnodes too high can lead to unpredictable results.
|
[Feb 08 2001]
|